/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ses;

import Entity.PaymentDetails;
import Entity.PaymentDetailsPK;
import ses.exceptions.NonexistentEntityException;
import ses.exceptions.PreexistingEntityException;
import ses.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entity.PaymentRate;
import Entity.Accounts;
import javax.transaction.UserTransaction;

/**
 *
 * @author Huu Vinh
 */
public class PaymentDetailsJpaController implements Serializable {

    public PaymentDetailsJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(PaymentDetails paymentDetails) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (paymentDetails.getPaymentDetailsPK() == null) {
            paymentDetails.setPaymentDetailsPK(new PaymentDetailsPK());
        }
        paymentDetails.getPaymentDetailsPK().setAccId(paymentDetails.getAccounts().getAccId());
        paymentDetails.getPaymentDetailsPK().setPaymentId(paymentDetails.getPaymentRate().getPaymentId());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            PaymentRate paymentRate = paymentDetails.getPaymentRate();
            if (paymentRate != null) {
                paymentRate = em.getReference(paymentRate.getClass(), paymentRate.getPaymentId());
                paymentDetails.setPaymentRate(paymentRate);
            }
            Accounts accounts = paymentDetails.getAccounts();
            if (accounts != null) {
                accounts = em.getReference(accounts.getClass(), accounts.getAccId());
                paymentDetails.setAccounts(accounts);
            }
            em.persist(paymentDetails);
            if (paymentRate != null) {
                paymentRate.getPaymentDetailsList().add(paymentDetails);
                paymentRate = em.merge(paymentRate);
            }
            if (accounts != null) {
                accounts.getPaymentDetailsList().add(paymentDetails);
                accounts = em.merge(accounts);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findPaymentDetails(paymentDetails.getPaymentDetailsPK()) != null) {
                throw new PreexistingEntityException("PaymentDetails " + paymentDetails + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(PaymentDetails paymentDetails) throws NonexistentEntityException, RollbackFailureException, Exception {
        paymentDetails.getPaymentDetailsPK().setAccId(paymentDetails.getAccounts().getAccId());
        paymentDetails.getPaymentDetailsPK().setPaymentId(paymentDetails.getPaymentRate().getPaymentId());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            PaymentDetails persistentPaymentDetails = em.find(PaymentDetails.class, paymentDetails.getPaymentDetailsPK());
            PaymentRate paymentRateOld = persistentPaymentDetails.getPaymentRate();
            PaymentRate paymentRateNew = paymentDetails.getPaymentRate();
            Accounts accountsOld = persistentPaymentDetails.getAccounts();
            Accounts accountsNew = paymentDetails.getAccounts();
            if (paymentRateNew != null) {
                paymentRateNew = em.getReference(paymentRateNew.getClass(), paymentRateNew.getPaymentId());
                paymentDetails.setPaymentRate(paymentRateNew);
            }
            if (accountsNew != null) {
                accountsNew = em.getReference(accountsNew.getClass(), accountsNew.getAccId());
                paymentDetails.setAccounts(accountsNew);
            }
            paymentDetails = em.merge(paymentDetails);
            if (paymentRateOld != null && !paymentRateOld.equals(paymentRateNew)) {
                paymentRateOld.getPaymentDetailsList().remove(paymentDetails);
                paymentRateOld = em.merge(paymentRateOld);
            }
            if (paymentRateNew != null && !paymentRateNew.equals(paymentRateOld)) {
                paymentRateNew.getPaymentDetailsList().add(paymentDetails);
                paymentRateNew = em.merge(paymentRateNew);
            }
            if (accountsOld != null && !accountsOld.equals(accountsNew)) {
                accountsOld.getPaymentDetailsList().remove(paymentDetails);
                accountsOld = em.merge(accountsOld);
            }
            if (accountsNew != null && !accountsNew.equals(accountsOld)) {
                accountsNew.getPaymentDetailsList().add(paymentDetails);
                accountsNew = em.merge(accountsNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                PaymentDetailsPK id = paymentDetails.getPaymentDetailsPK();
                if (findPaymentDetails(id) == null) {
                    throw new NonexistentEntityException("The paymentDetails with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(PaymentDetailsPK id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            PaymentDetails paymentDetails;
            try {
                paymentDetails = em.getReference(PaymentDetails.class, id);
                paymentDetails.getPaymentDetailsPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The paymentDetails with id " + id + " no longer exists.", enfe);
            }
            PaymentRate paymentRate = paymentDetails.getPaymentRate();
            if (paymentRate != null) {
                paymentRate.getPaymentDetailsList().remove(paymentDetails);
                paymentRate = em.merge(paymentRate);
            }
            Accounts accounts = paymentDetails.getAccounts();
            if (accounts != null) {
                accounts.getPaymentDetailsList().remove(paymentDetails);
                accounts = em.merge(accounts);
            }
            em.remove(paymentDetails);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<PaymentDetails> findPaymentDetailsEntities() {
        return findPaymentDetailsEntities(true, -1, -1);
    }

    public List<PaymentDetails> findPaymentDetailsEntities(int maxResults, int firstResult) {
        return findPaymentDetailsEntities(false, maxResults, firstResult);
    }

    private List<PaymentDetails> findPaymentDetailsEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from PaymentDetails as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public PaymentDetails findPaymentDetails(PaymentDetailsPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PaymentDetails.class, id);
        } finally {
            em.close();
        }
    }

    public int getPaymentDetailsCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from PaymentDetails as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
